package com.gewu.pm.ui.activity.clockin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.GoldBeanApprovalBean;
import com.gewu.pm.other.GridSpaceDecoration;
import com.gewu.pm.ui.activity.approval.RejectionRecordActivity;
import com.gewu.pm.ui.adapter.ImageGridAdapter;
import com.gewu.pm.ui.adapter.funds.GoldBeanApprovalAdapter;
import com.hjq.bar.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 补卡审批详情页面
 * 
 * type: 1=待审批，2=已通过，3=已驳回
 */
public final class MyMakeUpApprovalDetailActivity extends AppActivity {

    private TitleBar mTitleBar;
    private ImageView ivStatusBadge;
    private TextView tvApplicantValue;
    private TextView tvApplyTimeValue;
    private TextView tvReasonValue;
    private RecyclerView rvPhotos;
    private TextView tvRejectRecord;
    private RecyclerView rvApprovalFlow;
    private LinearLayout llBottomButtons;
    private TextView tvRejectInitiator;
    private TextView tvRejectPrevious;
    private TextView tvApprove;

    private ImageGridAdapter mPhotoAdapter;
    private final List<String> mPhotoList = new ArrayList<>();
    private GoldBeanApprovalAdapter mApprovalAdapter;

    private String mId;    // 申请ID
    private String mType;  // 状态类型：1=待审批，2=已通过，3=已驳回

    public static void start(Context context, String id, String type) {
        Intent intent = new Intent(context, MyMakeUpApprovalDetailActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.make_up_approval_detail_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.tb_title);
        ivStatusBadge = findViewById(R.id.iv_status_badge);
        tvApplicantValue = findViewById(R.id.tv_applicant_value);
        tvApplyTimeValue = findViewById(R.id.tv_apply_time_value);
        tvReasonValue = findViewById(R.id.tv_reason_value);
        rvPhotos = findViewById(R.id.rv_photos);
        tvRejectRecord = findViewById(R.id.tv_reject_record);
        rvApprovalFlow = findViewById(R.id.rv_approval_flow);
        llBottomButtons = findViewById(R.id.ll_bottom_buttons);
        tvRejectInitiator = findViewById(R.id.tv_reject_initiator);
        tvRejectPrevious = findViewById(R.id.tv_reject_previous);
        tvApprove = findViewById(R.id.tv_approve);

        // 设置照片RecyclerView
        rvPhotos.setLayoutManager(new GridLayoutManager(this, 3));
        rvPhotos.addItemDecoration(new GridSpaceDecoration((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics())));
        mPhotoAdapter = new ImageGridAdapter(this);
        rvPhotos.setAdapter(mPhotoAdapter);

        // 设置审批流程RecyclerView
        rvApprovalFlow.setLayoutManager(new LinearLayoutManager(this));
        mApprovalAdapter = new GoldBeanApprovalAdapter(this);
        rvApprovalFlow.setAdapter(mApprovalAdapter);

        // 设置点击事件
        setOnClickListener(tvRejectRecord, tvRejectInitiator, tvRejectPrevious, tvApprove);
    }

    @Override
    protected void initData() {
        // 获取Intent参数
        mId = getIntent().getStringExtra("id");
        mType = getIntent().getStringExtra("type");

        // 根据type设置状态标签和底部按钮
        setupUIByType();

        // 加载模拟数据
        loadMockData();
    }

    /**
     * 根据type设置UI
     */
    private void setupUIByType() {
        switch (mType) {
            case "1":
                // 待审批
                ivStatusBadge.setImageResource(R.drawable.approval_progress);
                llBottomButtons.setVisibility(View.VISIBLE);
                tvRejectRecord.setVisibility(View.GONE);
                break;
            case "2":
                // 已通过
                ivStatusBadge.setImageResource(R.drawable.approval_pass);
                llBottomButtons.setVisibility(View.GONE);
                tvRejectRecord.setVisibility(View.GONE);
                break;
            case "3":
                // 已驳回
                ivStatusBadge.setImageResource(R.drawable.approval_reject);
                llBottomButtons.setVisibility(View.GONE);
                tvRejectRecord.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 加载模拟数据
     */
    private void loadMockData() {
        // 基本信息
        tvApplicantValue.setText("陈文函");
        tvApplyTimeValue.setText("2025-08-27 周三 07:00");
        tvReasonValue.setText("该区域为补卡事由内容");

        // 照片（模拟1张图片）
        mPhotoList.add("https://example.com/photo.jpg");
        mPhotoAdapter.setData(mPhotoList);

        // 审批流程数据
        List<GoldBeanApprovalBean> approvalList = new ArrayList<>();
        
        switch (mType) {
            case "1":
                // 待审批
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "瞿辉·已提交", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "陈浩·审核通过", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "熊文尧·审核通过", 1));
                approvalList.add(new GoldBeanApprovalBean("", "胡凌·待审批", 0));
                approvalList.add(new GoldBeanApprovalBean("", "尹法德", 0));
                approvalList.add(new GoldBeanApprovalBean("", "邹彦", 0));
                break;
            case "2":
                // 已通过
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "瞿辉·已提交", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "陈浩·审核通过", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "熊文尧·审核通过", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "胡凌·审核通过", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "尹法德", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "邹彦", 1));
                break;
            case "3":
                // 已驳回
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "瞿辉·已提交", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "陈浩·审核通过", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "熊文尧·审核通过", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "胡凌·驳回发起人", 2));
                approvalList.add(new GoldBeanApprovalBean("", "尹法德", 0));
                approvalList.add(new GoldBeanApprovalBean("", "邹彦", 0));
                break;
        }

        mApprovalAdapter.setData(approvalList);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_reject_record) {
            // 查看驳回记录
            RejectionRecordActivity.start(getActivity());
        } else if (viewId == R.id.tv_reject_initiator) {
            // 驳回发起人
            toast("驳回发起人");
        } else if (viewId == R.id.tv_reject_previous) {
            // 驳回上一级
            toast("驳回上一级");
        } else if (viewId == R.id.tv_approve) {
            // 审核通过
            toast("审核通过");
        }
    }
}
